import { _decorator, Component, director, find, Input, Label, Node, Vec3 } from 'cc';
import { loading } from './loading';
import { store } from './store';
import { Tween } from 'cc';
import { RigidBody2D } from 'cc';
import { AudioManager } from './AudioManager';
const { ccclass, property } = _decorator;

@ccclass('GameOver')
export class GameOver extends Component {
    // 遮罩
    public mask:Node = null
    // 成绩面板
    public panel:Node = null
    // 得分label
    public score:Label = null
    // 最高成绩label
    public best:Label = null
    // ok按钮
    public okBtn:Node = null
    // 小鸟
    birdRgb:Component = null
    protected onEnable(): void {
        this.panel = this.node.getChildByName('panel')
        this.score = this.panel.getChildByName('score').getComponent(Label)
        this.best = this.panel.getChildByName('best').getComponent(Label)
        this.okBtn = this.node.getChildByName('okBtn')
        this.mask = this.node.parent.getChildByName('mask')
        this.birdRgb = this.node.parent.getChildByName('bird').getComponent(RigidBody2D)
        this.score.string = store.score.toString()
        this.best.string = store.bestScore.toString()
        // 为ok按钮添加点击事件
        this.okBtn.on(Input.EventType.TOUCH_START,this.startAgain,this)
        // 小鸟先静止
        this.birdRgb.enabled = false
        // 屏幕闪动
        new Tween(this.mask)
        .to(0.1,{ alpha:1 })
        .to(0.5,{ alpha:0 })
        .call(() => {
            // 播放音频
            AudioManager.instance.playEffect('die')
            // 小鸟继续移动
            this.birdRgb.enabled = true
        })
        .start()
    }
    start() {

    }
    // 重新开始游戏
    startAgain(){
        director.loadScene('gameReady')
    }
    update(deltaTime: number) {
        let pos = this.panel.getPosition()
        // 小于屏幕高度一半
        if(pos.y <= this.node.positionY){
            this.panel.setPosition(new Vec3(pos.x,pos.y + 10,0))
        }
    }
}


